﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagChina.Basic.CCT.PLC;
using HomagGroup.Base.Communication.Items;
using Proxy.Enum;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;

namespace Proxy
{
    public class InfeedTransportHandle
    {
        SearchInfoHandle Handle_Search;
        ModifyInfoHandle Handle_Modify;
        InsertInfoHandle Handle_Insert;
        PlcHandle Handle_Plc;

        public InfeedTransportHandle()
        {
            Handle_Search = new SearchInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_Plc = new PlcHandle();
        }

        public void Position_Transport(string Location)
        {
            Item Item_OrderState = null;
            Item Item_Reference = null;
            Item Item_Direction = null;
            T_BoardInfo t_Board = new T_BoardInfo();
            HomagChina.Basic.CCT.Db.CCT_Db.T_ProjectOrder t_ProjectOrder = new HomagChina.Basic.CCT.Db.CCT_Db.T_ProjectOrder();
            var log = SerilogHandle.Logger_P35;
            var Direction = 10;
            var mprName = string.Empty;
            var feature = string.Empty;
            float? width = 0;
            var mirrorImage = 0;
            do
            {
            Start:
                Thread.Sleep(1000);
                try
                {
                    if (Globle.isInfeedActive)
                    {
                        switch (Location)
                        {
                            case "20":
                                Item_OrderState = PlcItems.Item_S2_P1_OrderState;
                                Item_Reference = PlcItems.Item_S2_P1_In_Reference;
                                Item_Direction = PlcItems.Item_S2_P1_Out_Direction;
                                log = SerilogHandle.Logger_P20;
                                break;
                            default:
                                break;
                        }

                        var orderState = (int)(Handle_Plc.Read(Item_OrderState, "Int"));
                        if (!orderState.Equals((int)EnumOfOrderState.Plc请求数据)
                            && !orderState.Equals((int)EnumOfOrderState.Plc完成任务))
                        {
                            goto Start;
                        }

                        var Reference = Handle_Plc.Read(Item_Reference, "String").ToString();
                        t_Board = Handle_Search.GetTransOnLineInfo(Reference, Location);
                        if (t_Board == null)
                        {
                            goto Start;
                        }

                        if (Globle.OpreationMode.Equals("Auto"))
                        {
                            mprName = t_Board.MprName;
                            width = t_Board.Width;
                            feature = t_Board.Feature;
                            mirrorImage = (int)t_Board.PA_Angle;
                        }
                        else
                        {
                            t_ProjectOrder = Handle_Search.GetProjectOrderDetail();
                            if (t_ProjectOrder == null)
                            {
                                goto Start;
                            }
                            mprName = t_ProjectOrder.MprName;
                            width = t_ProjectOrder.Width;
                            feature = "B";
                            mirrorImage = (int)t_Board.PA_Angle;
                        }
                        log.Information("Reference :" + Reference);
                        log.Information("orderState :" + orderState);

                        switch (orderState)
                        {
                            case (int)EnumOfOrderState.Plc完成任务:
                            Write2Plc:
                                ResultCode WriteFlag = Handle_Plc.Write(Item_OrderState, (int)EnumOfOrderState.CCT完成任务);
                                if (WriteFlag != ResultCode.Success)
                                {
                                    goto Write2Plc;
                                }
                                Handle_Modify.ModifyTransState(Reference);
                                break;
                            case (int)EnumOfOrderState.Plc请求数据:
                                try
                                {
                                    var OnLineInfo = Handle_Search.GetTransOnLineInfo();
                                    var positionLookup = OnLineInfo.ToLookup(s => s.Location);
                                    switch (Location)
                                    {
                                        default:
                                            Direction = 10;
                                            goto Plc;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    log.Error(ex.Message);
                                    goto Start;
                                }
                            Plc:
                                Handle_Plc.Write(Item_Direction, Direction);
                                Handle_Plc.Write(Item_OrderState, (int)EnumOfOrderState.CCT下发任务);
                                break;
                        }
                        goto Start;
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                    goto Start;
                }
            } while (true);
        }
    }
}